﻿using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using ProcelNasEscolas.Model;
using System.IO;
using NHibernate.Tool.hbm2ddl;

namespace ProcelNasEscolas.Data
{
    public class AutoMappingConfiguration
    {
        private const string DbFile = "firstProgram.db";

        public static ISessionFactory CreateConfiguration()
        {
            return Fluently.Configure()
                .Database(SQLiteConfiguration.Standard
                    .UsingFile(DbFile))
                    .Mappings(m =>
                        m.AutoMappings.Add(
                        AutoMap.AssemblyOf<Aluno>()
                        .Where(t => t.Namespace == "ProcelNasEscolas.Model")
                        .Override<RespostaAlternativa>(map =>
                        {
                            map.HasMany(x => x.valor)
                               .Element("BoolList").AsBag();
                        })
                        //.Override<IQuestionario>(map =>
                        //{
                        //    map.HasMany(x => x.Questoes).Cascade.AllDeleteOrphan();
                        //})
                        //.Override<FichaCadastroDaEscola>(map =>
                        //{
                        //    map.HasMany(x => x.ProfessoresCapacitados).Cascade.AllDeleteOrphan();
                        //})
                        //.Override<FichaAcompanhamentoEscola>(map =>
                        //{
                        //    map.HasMany(x => x.ProfessoresCapacitados).Cascade.AllDeleteOrphan();
                        //})
                        ))

                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
        }

        private static void BuildSchema(Configuration config)
        {
            //if (File.Exists(DbFile))
            //    File.Delete(DbFile);

            //new SchemaExport(config)
            // .Create(false, true);
        }
    }
}